package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.LabelprintFormMap;
import com.iemes.entity.ProductreturnFormMap;
import com.iemes.entity.ReckonformFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.entity.WarehouseStockFormMap;
import com.iemes.mapper.warehouse.WarehouseStockMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/warehousestock_form/")
@Controller
public class Warehousestock_FormController extends BaseController {

	@Inject
	private WarehouseStockMapper warehouseStockMapper;

	// 查看进销存报表，显示按钮。
	@RequestMapping("warehousestock_form")
	public String warehousestock_form(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/warehousestock_form_list";
	}

	// 进销存报表分页
	@ResponseBody
	@RequestMapping("warehousestock_form_findByPage")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="进销存报表",methods="进销存报表-新增进销存报表")//凡需要处理业务逻辑的.都需要记录操作日志
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		//定义当前月的总天数
        int maxCurrentMonthDay=0;
		Date currentTime = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat month=new SimpleDateFormat("yyyy-MM");
		//每月第一天
		Calendar  claendar1  =  Calendar.getInstance();
		claendar1.set(Calendar.DAY_OF_MONTH, 1);
		//每月最后一天
		Calendar calendar2=Calendar.getInstance();
		maxCurrentMonthDay=calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
		calendar2.set(Calendar.DAY_OF_MONTH, maxCurrentMonthDay);
		//今日日期
		String dateString = sdf.format(currentTime);
		//本月月份
		String datemoth = month.format(currentTime);
		WarehouseStockFormMap warehouseStockFormMap = getFormMap(WarehouseStockFormMap.class);
		List<Map<String, Object>> stockAll=warehouseStockMapper.getAll(warehouseStockFormMap);
		double enterReckonTotal=0,outNumberTotal=0;
		for (Map<String, Object> map : stockAll) {
			//填写入库报表日期
			String edate = map.get("edate").toString(); 
			edate=edate.substring(0,edate.lastIndexOf("-"));
			if(map.get("enterReckon")!=null && map.get("price")!=null && map.get("outTotal")!=null){
				int enterReckon = Integer.parseInt(map.get("enterReckon").toString());
				double price = Double.parseDouble(map.get("price").toString());
				int outTotal = Integer.parseInt(map.get("outTotal").toString());
				//计算每种物料的收入金额
				enterReckonTotal=enterReckon*price;
				//计算每种物料的发出金额
				outNumberTotal=outTotal*price;
				map.put("currentInNumber", enterReckon);
				map.put("currentInPrice", price);
				map.put("currentInMoney", enterReckonTotal);
				map.put("currentOutNumber", outTotal);
				map.put("currentOutPrice", price);
				map.put("currentOutMoney", outNumberTotal);
				baseMapper.editEntity(map);
				//本月最后一天
				if((dateString.equals(calendar2.getTime())) && (edate.equals(datemoth)) ){
					map.put("lastNumber", outTotal);
					map.put("lastPrice", price);
					map.put("lastMoney",outNumberTotal);
					map.put("currentOutNumber", 0);
					map.put("currentOutPrice", 0);
					map.put("currentOutMoney", 0);
					baseMapper.editEntity(map);
				}
				//下月第一天
				if((dateString.equals(claendar1.getTime())) && (!edate.equals(datemoth)) ){
					map.put("initialNumber", enterReckon);
					map.put("initialPrice", price);
					map.put("initialMoney",enterReckonTotal);
					map.put("currentInNumber", 0);
					map.put("currentInPrice", 0);
					map.put("currentInMoney", 0);
					baseMapper.editEntity(map);
				}
			}
		}
		LuogicPaging(pageNow, pageSize, stockAll);
		return pageView;
	}

	// 编辑进销存报表
	@RequestMapping("warehousestock_form_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id获得进销存报表
			WarehouseStockFormMap stock=warehouseStockMapper.getById(id);
			model.addAttribute("stock",stock);
		}
		return Common.BACKGROUND_PATH + "/warehouse/warehousestock_form_edit";
	}

	// 修改进销存报表
	@ResponseBody
	@RequestMapping(value = "warehousestock_form_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "进销存报表", methods = "查看进销存报表-修改进销存报表") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		WarehouseStockFormMap warehouseStockFormMap=getFormMap(WarehouseStockFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		warehouseStockFormMap.put("fillDate", sdf.format(date));
		warehouseStockFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		baseMapper.editEntity(warehouseStockFormMap);
		return "success";
	}	
}








